package com.zijie.hosptal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.aop.Log;
import com.zijie.hosptal.common.Result;
import com.zijie.hosptal.pojo.dto.Account;
import com.zijie.hosptal.pojo.po.Admin;
import com.zijie.hosptal.service.AdminService;
import com.zijie.hosptal.util.JwtUtils;
import com.zijie.hosptal.util.Md5Util;
import com.zijie.hosptal.util.ThreadLocalUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    private AdminService adminService;

    @Autowired
    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @GetMapping
    public Result<?> getAllAdminList(Integer page, Integer limit, Admin admin) {
        Page<Admin> page1 = Page.of(page, limit);
        page1 = adminService.list(page1, admin);
        return Result.success(page1);
    }
    //添加
    @PostMapping
    public Result<?> addAdmin(@Validated @RequestBody Admin admin, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean flag = adminService.save(admin);
        return Result.success(flag);
    }
    //删除
    @DeleteMapping("/{id}")
    public Result<?> deleteAdmin(@PathVariable Integer id){
        String username = adminService.removeById(id);
        return Result.success(username);
    }
    //修改
    @PutMapping
    public Result<?> updateAdmin(@Validated @RequestBody Admin admin,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            // 获取第一个错误信息
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            return Result.fail(errorMessage);
        }
        boolean flag = adminService.updateById(admin);
        return Result.success(flag);
    }
    //密码
    @PostMapping("/verifyPassword")
    public Result<?> verifyPassword(@RequestBody Admin admin){
        Admin admin1 = adminService.getById(admin.getId());
        boolean equals = admin1.getPassword().equals(Md5Util.getMD5String(admin.getPassword()));
        return equals?Result.success("验证成功"):Result.fail("密码错误");
    }
    //登录
    @PostMapping("/login")
    public Result<?> login(@RequestBody Account account, HttpSession session){
        String captcha = account.getCaptcha();
        String captcha1 = session.getAttribute("captcha").toString().toLowerCase();
        System.out.println("captcha1: " + captcha1);
        System.out.println(captcha.toLowerCase());
        if (!captcha.equals(captcha1)){
            return Result.fail("验证码错误");
        }
        Admin admin1=adminService.getByUsername(account);
        if (admin1==null){
            return Result.fail("用户不存在");
        }
        if (admin1.getPassword().equals(Md5Util.getMD5String(account.getPassword()))){
            Map map = new HashMap<>();
            map.put("name",account.getUsername());
            String token = JwtUtils.generateJwt(map);
            account.setToken(token);
            return Result.success(account);
        }
        return Result.fail("密码错误");
    }

    //获取当前用户的登录信息
    @GetMapping("/getLogin")
    public Result<?> getLogin(HttpServletRequest request){
        String header = request.getHeader("token");
//        System.out.println(header);
        if (StringUtils.hasText( header)){
            Claims claims = JwtUtils.parseJWT(header);
            //用户名字
            String name = (String) claims.get("name");
            return Result.success(name);
        }
        return Result.fail("请先登录");
    }
}
